package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.util.Md5Util;
import com.amigo.online.provider.manager.model.entity.User;
import com.amigo.online.provider.manager.service.RedisService;
import com.amigo.online.provider.manager.service.UserService;
import com.amigo.online.provider.manager.util.UserException;
import com.amigo.online.provider.manager.mapper.UserMapper;

@Service
public class UserServiceImpl extends BaseService<User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private UserService userService;

    @Override
    public List<User> getUserList(User user) {
        // if (user.getTimetype()!=null) {
        // TimestampUtil.setQueryTime(user);
        // }
        return userMapper.findUserList(user);
    }

    @Override
    public boolean insertionUser(User user) {

        if (userService.selectAccount(user.getAccount())) {
            user.setPasswordHash(Md5Util.encrypt(user.getPassword()));
            if (userMapper.insertionUser(user) <= 0) {

                throw new UserException(StatusCode.NEW_USER_FAILED);
            } else {
                return true;
            }
        } else {
            throw new UserException(StatusCode.ACCOUNT_EXIST);
        }

    }

    @Override
    public boolean selectAccount(String account) {
        List<User> uss = userMapper.selectAccount(account);
        if (uss.isEmpty() || uss.size() == 0) {
            return true;
        }

        return false;
    }

    @Override
    public boolean updateUser(User user) {
        if (user.getPassword() != null && user.getPassword() != "") {
            user.setPasswordHash(Md5Util.encrypt(user.getPassword()));
        }
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean updateType(User user) {
        return userMapper.updateType(user) > 0;
    }

}
